package com.gmrz.uap.globalist;

import com.gmrz.uap.cache.AmmsCacheUtil;
import com.gmrz.uap.cfg.SystemConst;
import com.gmrz.uap.common.ConditionConst;
import com.gmrz.uap.common.ControllerSupport;
import com.gmrz.uap.equipment.EquipmentService;
import com.gmrz.uap.model.Equipment;
import com.gmrz.uap.model.EquipmentForbid;
import com.gmrz.uap.model.Vendor;
import com.gmrz.uap.model.VendorAllow;
import com.gmrz.uap.operlog.OperLogService;
import com.gmrz.uap.resource.ResourceInterceptor;
import com.gmrz.uap.util.Identities;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import org.apache.log4j.Logger;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * @PACKAGE_NAME: com.gmrz.uap.globalist
 * @ClassName: GlobalBlackWhiteListController
 * @Description:
 * @Date: 2020-04-17 17:10
 * @Author: liyl@gmrz-bj.com
 **/
public class GlobalBlackWhiteListController extends ControllerSupport {
    private final static Logger LOG = Logger.getLogger(GlobalBlackWhiteListController.class);

    @Before(ResourceInterceptor.class)
    public void index() {

    }

    public void list() {
        int page = getParaToInt(SystemConst.DATATABLE_PAGINATE_OFFSET, SystemConst.DATATABLE_PAGINATE_OFFSET_DEFAULT);
        int rows = getParaToInt(SystemConst.DATATABLE_PAGINATE_LIMIT, SystemConst.DATATABLE_PAGINATE_LIMIT_DEFAULT);

        String sort = getPara(SystemConst.DATATABLE_PAGINATE_SORT, "equ_model");
        String order = getPara(SystemConst.DATATABLE_PAGINATE_ORDER, SystemConst.DATATABLE_PAGINATE_ORDER_DEFAULT);

        Equipment equipment = getModel(Equipment.class);

        Page<Equipment> descPage = EquipmentService.me.findEquipmentListForPaginate(equipment, page, rows, sort, order);

        result.put(SystemConst.DATATABLE_PAGINATE_TOTAL, descPage.getTotalRow());
        result.put(SystemConst.DATATABLE_PAGINATE_ROWS, descPage.getList());

        render(responseJsonRender(result));
    }


    public void globalAllow() {
        render("globalAllow" + SystemConst.VIEW_EXTENDSION);
    }


    public void findVendorListForPaginateNotInAllow() {
        int page = getParaToInt(SystemConst.DATATABLE_PAGINATE_OFFSET, SystemConst.DATATABLE_PAGINATE_OFFSET_DEFAULT);
        int rows = getParaToInt(SystemConst.DATATABLE_PAGINATE_LIMIT, SystemConst.DATATABLE_PAGINATE_LIMIT_DEFAULT);

        String sort = getPara(SystemConst.DATATABLE_PAGINATE_SORT, "vendor_name");
        String order = getPara(SystemConst.DATATABLE_PAGINATE_ORDER, SystemConst.DATATABLE_PAGINATE_ORDER_DEFAULT);

        Vendor vendor = getModel(Vendor.class);
        String tenantId = ConditionConst.CONDITTION_GLOBAL_BLACK_WHITE_TENANT;
        Page<Vendor> descPage = EquipmentService.me.findVendorListForPaginateNotInAllow(vendor, tenantId, page, rows, sort, order);

        result.put(SystemConst.DATATABLE_PAGINATE_TOTAL, descPage.getTotalRow());
        result.put(SystemConst.DATATABLE_PAGINATE_ROWS, descPage.getList());

        render(responseJsonRender(result));
    }


    public void findVendorAllowListForPaginate() {
        int page = getParaToInt(SystemConst.DATATABLE_PAGINATE_OFFSET, SystemConst.DATATABLE_PAGINATE_OFFSET_DEFAULT);
        int rows = getParaToInt(SystemConst.DATATABLE_PAGINATE_LIMIT, SystemConst.DATATABLE_PAGINATE_LIMIT_DEFAULT);

        String sort = getPara(SystemConst.DATATABLE_PAGINATE_SORT, "vendor_name");
        String order = getPara(SystemConst.DATATABLE_PAGINATE_ORDER, SystemConst.DATATABLE_PAGINATE_ORDER_DEFAULT);

        VendorAllow vendorAllow = getModel(VendorAllow.class);

        String tenantId = ConditionConst.CONDITTION_GLOBAL_BLACK_WHITE_TENANT;
        vendorAllow.setTenantId(tenantId);

        Page<VendorAllow> descPage = EquipmentService.me.findVendorAllowListForPaginate(vendorAllow, page, rows, sort, order);

        result.put(SystemConst.DATATABLE_PAGINATE_TOTAL, descPage.getTotalRow());
        result.put(SystemConst.DATATABLE_PAGINATE_ROWS, descPage.getList());

        render(responseJsonRender(result));
    }


    public void globalAllowSave() {
        String venIds = getPara("venIds");
        String equIds = getPara("equIds");
        String equVerIds = getPara("equVerIds");
        String equModels = getPara("equModels");
        String equVersions = getPara("equVersions");
        String vendorNames = getPara("vendorNames");
        String tenantId = ConditionConst.CONDITTION_GLOBAL_BLACK_WHITE_TENANT;

        try {
            VendorAllow vendorAllow;
            String[] venIdArr = venIds.split(SystemConst._DEFAULT_SEPARATOR_COMMA);
            String[] equIdArr = equIds.split(SystemConst._DEFAULT_SEPARATOR_COMMA);
            String[] equVerIdArr = equVerIds.split(SystemConst._DEFAULT_SEPARATOR_COMMA);
            for (int i = 0; i < venIdArr.length; i++) {

                vendorAllow = new VendorAllow();
                vendorAllow.setTenantId(tenantId);
                vendorAllow.setVenId(venIdArr[i]);
                vendorAllow.setVfId(Identities.uuid2());
                if (!ConditionConst.CONDITTION_FORBID_EQUID.equals(equIdArr[i]))
                    vendorAllow.setEquId(equIdArr[i]);
                if (!ConditionConst.CONDITTION_FORBID_EQUVERSIONID.equals(equVerIdArr[i]))
                    vendorAllow.setEquVersionId(equVerIdArr[i]);
                flag = vendorAllow.save();

                if (!flag) {
                    break;
                }
            }

            if (flag) {
                message = "增加厂商型号版本全局白名单成功！";
                String cacheKey = AmmsCacheUtil.CACHE_VENDOR_ALLOW + "." + tenantId;
                LOG.info("厂商型号版本全局白名单缓存键值 => " + cacheKey);
                message = AmmsCacheUtil.deleteIfSuccess(cacheKey, message);
            } else {
                message = "增加厂商型号版本全局白名单失败，请重试！";
            }

        } catch (JedisConnectionException jedisConnectionException) {
            message += getRes().get("redis.connection.failed");
            LOG.error(message, jedisConnectionException);
        }

        //增加系统操作日志
        OperLogService.me.addOperLog("增加厂商型号版本全局白名单", vendorNames+" | "+equModels+" | "+equVersions, flag, message, getRequest());

        result.put(SystemConst.RESPONSE_RESULT_FLAG, flag);
        result.put(SystemConst.RESPONSE_RESULT_MESSAGE, message);
        render(responseJsonRender(result));
    }


    public void globalAllowRemove() {
        String ids = getPara("ids");
        String vendorNames = getPara("vendorNames");
        String equModels = getPara("equModels");
        String equVersions = getPara("equVersions");
        String tenantId = ConditionConst.CONDITTION_GLOBAL_BLACK_WHITE_TENANT;

        try {
            VendorAllow vendorAllow;
            String[] idArr = ids.split(SystemConst._DEFAULT_SEPARATOR_COMMA);
            for (int i = 0; i < idArr.length; i++) {

                vendorAllow = new VendorAllow();
                flag = vendorAllow.deleteById(idArr[i]);

                if (!flag) {
                    break;
                }
            }

            if (flag) {
                message = "删除厂商型号版本全局白名单成功！";
                String cacheKey = AmmsCacheUtil.CACHE_VENDOR_ALLOW + "." + tenantId;
                LOG.info("厂商型号版本全局白名单缓存键值 => " + cacheKey);
                message = AmmsCacheUtil.deleteIfSuccess(cacheKey, message);
            } else {
                message = "删除厂商型号版本全局白名单失败，请重试！";
            }
        } catch (JedisConnectionException jedisConnectionException) {
            message += getRes().get("redis.connection.failed");
            LOG.error(message, jedisConnectionException);
        }

        //增加系统操作日志
        OperLogService.me.addOperLog("删除厂商型号版本全局白名单", vendorNames+" | "+equModels+" | "+equVersions, flag, message, getRequest());

        result.put(SystemConst.RESPONSE_RESULT_FLAG, flag);
        result.put(SystemConst.RESPONSE_RESULT_MESSAGE, message);
        render(responseJsonRender(result));
    }




    public void globalForbid() {
        render("globalForbid" + SystemConst.VIEW_EXTENDSION);
    }


    public void findListForPaginateNotInForbid() {
        int page = getParaToInt(SystemConst.DATATABLE_PAGINATE_OFFSET, SystemConst.DATATABLE_PAGINATE_OFFSET_DEFAULT);
        int rows = getParaToInt(SystemConst.DATATABLE_PAGINATE_LIMIT, SystemConst.DATATABLE_PAGINATE_LIMIT_DEFAULT);

        String sort = getPara(SystemConst.DATATABLE_PAGINATE_SORT, "vendor_name");
        String order = getPara(SystemConst.DATATABLE_PAGINATE_ORDER, SystemConst.DATATABLE_PAGINATE_ORDER_DEFAULT);

        Equipment equipment = getModel(Equipment.class);

        String tenantId = ConditionConst.CONDITTION_GLOBAL_BLACK_WHITE_TENANT;
        Page<Equipment> descPage = EquipmentService.me.findListForPaginateNotInForbid(equipment, tenantId, page, rows, sort, order);

        result.put(SystemConst.DATATABLE_PAGINATE_TOTAL, descPage.getTotalRow());
        result.put(SystemConst.DATATABLE_PAGINATE_ROWS, descPage.getList());

        render(responseJsonRender(result));
    }


    public void findglobalForbidListForPaginate() {
        int page = getParaToInt(SystemConst.DATATABLE_PAGINATE_OFFSET, SystemConst.DATATABLE_PAGINATE_OFFSET_DEFAULT);
        int rows = getParaToInt(SystemConst.DATATABLE_PAGINATE_LIMIT, SystemConst.DATATABLE_PAGINATE_LIMIT_DEFAULT);

        String sort = getPara(SystemConst.DATATABLE_PAGINATE_SORT, "vendor_name");
        String order = getPara(SystemConst.DATATABLE_PAGINATE_ORDER, SystemConst.DATATABLE_PAGINATE_ORDER_DEFAULT);

        EquipmentForbid equipmentForbid = getModel(EquipmentForbid.class);
        String tenantId = ConditionConst.CONDITTION_GLOBAL_BLACK_WHITE_TENANT;
        equipmentForbid.setTenantId(tenantId);


        Page<EquipmentForbid> descPage = EquipmentService.me.findEquipmentForbidListForPaginate(equipmentForbid, page, rows, sort, order);

        result.put(SystemConst.DATATABLE_PAGINATE_TOTAL, descPage.getTotalRow());
        result.put(SystemConst.DATATABLE_PAGINATE_ROWS, descPage.getList());

        render(responseJsonRender(result));
    }



    public void globalForbidSave() {
        String venIds = getPara("venIds");
        String equIds = getPara("equIds");
        String equVerIds = getPara("equVerIds");
        String equModels = getPara("equModels");
        String equVersions = getPara("equVersions");
        String vendorNames = getPara("vendorNames");
        String tenantId = ConditionConst.CONDITTION_GLOBAL_BLACK_WHITE_TENANT;

        try {
            EquipmentForbid equipmentForbid;
            String[] venIdArr = venIds.split(SystemConst._DEFAULT_SEPARATOR_COMMA);
            String[] equIdArr = equIds.split(SystemConst._DEFAULT_SEPARATOR_COMMA);
            String[] equVerIdArr = equVerIds.split(SystemConst._DEFAULT_SEPARATOR_COMMA);
            for (int i = 0; i < venIdArr.length; i++) {

                equipmentForbid = new EquipmentForbid();
                equipmentForbid.setTenantId(tenantId);
                if (!ConditionConst.CONDITTION_FORBID_EQUID.equals(equIdArr[i]))
                    equipmentForbid.setEquId(equIdArr[i]);
                if (!ConditionConst.CONDITTION_FORBID_EQUVERSIONID.equals(equVerIdArr[i]))
                    equipmentForbid.setEquVersionId(equVerIdArr[i]);
                equipmentForbid.setVenId(venIdArr[i]);
                equipmentForbid.setEaId(Identities.uuid2());
                flag = equipmentForbid.save();

                if (!flag) {
                    break;
                }
            }

            if (flag) {
                message = "增加厂商型号版本全局黑名单成功！";
                String cacheKey = AmmsCacheUtil.CACHE_EQUIPMENT_FORBID + "." + tenantId;
                LOG.info("厂商型号版本全局黑名单缓存键值 => " + cacheKey);
                message = AmmsCacheUtil.deleteIfSuccess(cacheKey, message);
            } else {
                message = "增加厂商型号版本全局黑名单失败，请重试！";
            }

        } catch (JedisConnectionException jedisConnectionException) {
            message += getRes().get("redis.connection.failed");
            LOG.error(message, jedisConnectionException);
        }

        //增加系统操作日志
        OperLogService.me.addOperLog("增加厂商型号版本全局黑名单", vendorNames+" | "+equModels+" | "+equVersions, flag, message, getRequest());

        result.put(SystemConst.RESPONSE_RESULT_FLAG, flag);
        result.put(SystemConst.RESPONSE_RESULT_MESSAGE, message);
        render(responseJsonRender(result));
    }


    public void globalForbidRemove() {
        String ids = getPara("ids");
        String vendorNames = getPara("vendorNames");
        String equModels = getPara("equModels");
        String equVersions = getPara("equVersions");
        String tenantId = ConditionConst.CONDITTION_GLOBAL_BLACK_WHITE_TENANT;

        try {
            EquipmentForbid equipmentForbid;
            String[] idArr = ids.split(SystemConst._DEFAULT_SEPARATOR_COMMA);
            for (int i = 0; i < idArr.length; i++) {

                equipmentForbid = new EquipmentForbid();
                flag = equipmentForbid.deleteById(idArr[i]);

                if (!flag) {
                    break;
                }
            }

            if (flag) {
                message = "删除厂商型号版本全局黑名单成功！";
                String cacheKey = AmmsCacheUtil.CACHE_EQUIPMENT_FORBID + "." + tenantId;
                LOG.info("厂商型号版本全局黑名单缓存键值 => " + cacheKey);
                message = AmmsCacheUtil.deleteIfSuccess(cacheKey, "删除厂商型号版本全局黑名单成功！");
            } else {
                message = "删除厂商型号版本全局黑名单失败，请重试！";
            }
        } catch (JedisConnectionException jedisConnectionException) {
            message += getRes().get("redis.connection.failed");
            LOG.error(message, jedisConnectionException);
        }

        //增加系统操作日志
        OperLogService.me.addOperLog("删除厂商型号版本全局黑名单", vendorNames+" | "+equModels+" | "+equVersions, flag, message, getRequest());

        result.put(SystemConst.RESPONSE_RESULT_FLAG, flag);
        result.put(SystemConst.RESPONSE_RESULT_MESSAGE, message);
        render(responseJsonRender(result));
    }



}
